<template>
  <div class="xtx-form">
    <div class="user-info">
      <img :src="avatar" alt="" />
      <p>Hi，{{ nickname }}欢迎来小兔鲜，完成绑定后可以QQ账号一键登录哦~</p>
    </div>
    <Form
      :validation-schema="mySchema"
      v-slot="{ errors }"
      autocomplete="off"
      class="xtx-form"
      ref="formRef"
    >
      <div class="xtx-form-item">
        <div class="field">
          <i class="icon iconfont icon-phone"></i>
          <Field
            :class="{ err: errors.mobile }"
            v-model="form.mobile"
            name="mobile"
            class="input"
            type="text"
            placeholder="绑定的手机号"
          />
        </div>
        <div v-if="errors.mobile" class="error">{{ errors.mobile }}</div>
      </div>
      <div class="xtx-form-item">
        <div class="field">
          <i class="icon iconfont icon-code"></i>
          <Field
            :class="{ err: errors.code }"
            v-model="form.code"
            name="code"
            class="input"
            type="text"
            placeholder="短信验证码"
          />
          <span class="code" @click="send">
            {{countTime === 0 ? '发送验证码' : `${countTime}s发送`}}
          </span>
        </div>
        <div v-if="errors.code" class="error">{{ errors.code }}</div>
      </div>
      <a href="javascript:;" class="submit" @click="doBind">立即绑定</a>
    </Form>
  </div>
</template>

<script>
// 获取qq头像和名称
// QC.api('get-user-info') 还是先调用check方法
import { Form, Field } from 'vee-validate'
import schema from '@/utils/vee-validate-schema'
import QC from 'qc'
import { onMounted, reactive, toRefs, ref } from 'vue'
import { userQQBindCode, userQQBindLogin } from '@/api/user'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { useCountDown } from '@/compstions'
export default {
  name: 'CallbackBind',
  components: {
    Form, Field
  },
  props: {
    unionId: {
      type: String
    }
  },
  setup (props) {
    // 获取qq头像用户名
    const qqInfo = reactive({
      nickname: '',
      avatar: ''
    })
    function loadQQInfo () {
      if (QC.Login.check()) {
        QC.api('get_user_info').success(res => {
          console.log('qq用户信息', res.data)
          qqInfo.nickname = res.data.nickname
          qqInfo.avatar = res.data.figureurl_qq_1
        })
      }
    }

    onMounted(() => {
      loadQQInfo()
    })

    // 表单数
    const form = reactive({
      mobile: '',
      code: ''
    })
    // 校验规则对象
    const mySchema = {
      mobile: schema.mobile,
      code: schema.code
    }

    // 短信验证服务流程
    // 1. 先保证手机号是填写正确的
    // 2. 在手机好正确的情况下发送有关验证码的接口请求
    // 3. 手机号接收到验证码进行填写 然后进行绑定操作 (123456)
    // 4. 在请求验证码接口发起之后 需要开启一个倒计时60 在60s内不要再次发送 等待 60s之后 可以再次发起请求

    const { countTime, start } = useCountDown(60)
    function send () {
      // 发送逻辑
      // 校验手机号
      const isPass = mySchema.mobile(form.mobile) // 手机号为空
      if (isPass === true) {
        // 发送验证码接口请求
        // 只有countTime为零才能再次发送 在倒计时内不能重复发送
        if (countTime.value === 0) {
          userQQBindCode(form.mobile).then(res => {
          // 开启倒计时
            start()
          })
        }
      }
    }

    // 立刻绑定
    const formRef = ref(null)
    const store = useStore()
    const router = useRouter()
    async function doBind () {
      // 1. 兜底校验
      // 2. 绑定接口的调用
      // yarn 4.0.3 {valid:false,errors:{}}  npm 4.0.3 true/false
      // rm -rf node_modules/
      const isPass = await formRef.value.validate()
      if (isPass) {
        // 调用正式绑定接口
        // 缺少openId
        userQQBindLogin({
          unionId: props.unionId,
          mobile: form.mobile,
          code: form.code
        }).then(res => {
          // 1.把res存入vuex
          store.commit('user/setProfile', res)
          // 2.跳转到首页
          router.push('/')
        })
      }
    }
    return {
      ...toRefs(qqInfo),
      mySchema,
      form,
      send,
      formRef,
      doBind,
      countTime
    }
  }
}
</script>

<style scoped lang='less'>
.user-info {
  width: 320px;
  height: 70px;
  margin: 0 auto;
  display: flex;
  background: #f2f2f2;
  align-items: center;
  padding: 0 10px;
  margin-bottom: 25px;
  img {
    background: #f2f2f2;
    width: 50px;
    height: 50px;
  }
  p {
    padding-left: 10px;
  }
}
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
